// pages/order/orderlist/orderlist.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab:[{name:"全部",typeid:0},{name:"待付款",typeid:1},{name:"待发货",typeid:2},{name:"待收货",typeid:3}],
    list:[],
    pageloadding:false,
    p:1,
    typeid:0,
    tabActive:0,
    loadding:false
  },
  getOrderlist:function(p){
    let index=this.data.tabActive;
    let typeid=this.data.tab[index].typeid;
    let data={token:app.globalData.token,p,typeid}
    app.requestJs('Order/orderlist',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        resData.forEach(item => {
          console.log(item);
          item.orderstatus_1=item.orderstatus.includes(1);
          item.orderstatus_2=item.orderstatus.includes(2);
          item.orderstatus_3=item.orderstatus.includes(3);
          item.orderstatus_4=item.orderstatus.includes(4);
          item.orderstatus_5=item.orderstatus.includes(5);
          item.orderstatus_6=item.orderstatus.includes(6);
          item.orderstatus_7=item.orderstatus.includes(7);
          item.orderstatus_8=item.orderstatus.includes(8);
          item.orderstatus_9=item.orderstatus.includes(9);
          item.orderstatus_10=item.orderstatus.includes(10);
        });
        this.setData({
          list:data.p==1?resData:[...this.data.list,...resData],
          pageloadding:resData.length>=10?false:true,
          p:data.p,
          loadding:false
        })
      }else app.utilJs.toast({ title: res.message });
    })
  },
  bindTab:function(e){
    if(this.data.loadding)return;
    let index=e.currentTarget.dataset.index;
    if(this.data.tabActive==index)return
    this.setData({
      tabActive:index,
      loadding:true
    })
    this.getOrderlist(1);
  },
  orderbind_before:function(e){
    let that=this;
    let index=e.currentTarget.dataset.index,
        obj=this.data.list[index],
        orderid=obj.orderid,
        orderstatus=e.currentTarget.dataset.typeid;
        console.log(e);
        console.log(obj);
    if(orderstatus==7)wx.navigateTo({url: '/pages/order/express_info/express_info?orderid='+orderid});
    else if(orderstatus==4){
      wx.navigateTo({
        url: '/pages/order/refund_info/refund_info?orderid='+orderid,
        events:{
          updateData:function(){
            that.getOrderlist(1);
          }
        }
      })
    }
    else {
      let data={orderid,orderstatus,token:app.globalData.token};
      if(data.orderstatus==10)data.ordersn=obj.ordersn;
      let msgContent='';
      if(data.orderstatus==3)msgContent="确定要取消订单吗";
      else if(data.orderstatus==6)msgContent="确定要确认收货吗";
      else if(data.orderstatus==9)msgContent="确定要删除订单吗";
      if(msgContent=='')that.orderStateBtn(data);
      else that.orderHandleModal(msgContent,data);
    }
  },
  orderHandleModal:function(msgContent,data){//提示
    let that=this;
    wx.showModal({
      title: '提示',
      content:msgContent,
      success (res) {
        if (res.confirm) {
          that.orderStateBtn(data);
        }
      }
    })
  },
  orderStateBtn:function(data){//订单聚合操作
    // console.log("gdsgs")
    // console.log(data)
    // return false
    app.requestJs("order/orderjuhehander",{data}).then((res) => {
      if (res.errcode == 1) {
        app.utilJs.toast({ title: res.message,icon:'success'});
        this.getOrderlist(1);
      } else {
        app.utilJs.toast({ title: res.message });
      }
    })
  },
  get_wxpayApi:function(e){
    let ordersn=e.currentTarget.dataset.ordersn;
    let data={token:app.globalData.token,ordersn};
    app.requestJs('Payment/getpay',{data}).then((res) => {
      if (res.errcode == 1) {
        this.order_wxPay(JSON.parse(res.data),ordersn);
      } else {
        app.utilJs.toast({ title: res.message });
      }
    })
  },
  order_wxPay:function(data,ordersn){//调起支付
    console.log(data);
    console.log(ordersn)
    let that=this;
    wx.requestPayment({
      timeStamp:data.timeStamp,
      nonceStr:data.nonceStr,
      package:data.package,
      signType:data.signType,
      paySign:data.paySign,
      success(res) {
        console.log(res);
        if(res.errMsg=='requestPayment:ok'){
          that.getOrderlist(1);
          setTimeout(()=>{
            app.utilJs.toast({ title:"支付成功" ,icon:'success'});
          },200)
        }
      },
      fail(error) {
        app.utilJs.toast({ title: '支付失败' });
      }
    })
  },
  orderInfoLink:function(e){
    let that=this;
    let ordersn=e.currentTarget.dataset.ordersn;
    wx.navigateTo({
      url: '/pages/order/orderinfo/orderinfo?ordersn='+ordersn,
      events:{
        updateData:function(){
          that.getOrderlist(1);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let index=options.index || 0;
    let failType = options.failType || 2 //下单的时候取消支付跳转过来，1为支付失败，2不处理
    if(failType == 1) {
      wx.showToast({
        title: '支付失败',
        icon: 'none'
      })
    }
    this.setData({
      tabActive:index
    })
    // 测试用
    // this.setData({
    //   list: [
    //     {
    //       ordersn: '297537693576945764',
    //       statusstr: '待付款',
    //       goodslist: [
    //         {
    //           id: 1,
    //           thumb: '/img/61.png',
    //           goods_name: '海南小台农芒果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         },{
    //           id: 2,
    //           thumb: '/img/61.png',
    //           goods_name: '海南贵妃芒果当季热带新鲜水果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         }
    //       ],
    //       order_amount: '39.80',
    //       orderstatus_3: true,
    //       orderstatus_2: true
    //     },{
    //       ordersn: '297537693576945764',
    //       statusstr: '待发货',
    //       goodslist: [
    //         {
    //           id: 1,
    //           thumb: '/img/61.png',
    //           goods_name: '海南小台农芒果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         },{
    //           id: 2,
    //           thumb: '/img/61.png',
    //           goods_name: '海南贵妃芒果当季热带新鲜水果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         }
    //       ],
    //       order_amount: '39.80',
    //     },{
    //       ordersn: '297537693576945764',
    //       statusstr: '待收货',
    //       goodslist: [
    //         {
    //           id: 1,
    //           thumb: '/img/61.png',
    //           goods_name: '海南小台农芒果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         },{
    //           id: 2,
    //           thumb: '/img/61.png',
    //           goods_name: '海南贵妃芒果当季热带新鲜水果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         }
    //       ],
    //       order_amount: '39.80',
    //       orderstatus_6: true
    //     },{
    //       ordersn: '297537693576945764',
    //       statusstr: '已完成',
    //       goodslist: [
    //         {
    //           id: 1,
    //           thumb: '/img/61.png',
    //           goods_name: '海南小台农芒果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         },{
    //           id: 2,
    //           thumb: '/img/61.png',
    //           goods_name: '海南贵妃芒果当季热带新鲜水果',
    //           specname: '100g',
    //           price: '19.90',
    //           goods_num: 1
    //         }
    //       ],
    //       order_amount: '39.80',
    //     }
    //   ]
    // })
    // return
    this.getOrderlist(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.pageloadding)return;
    let p=this.data.p+1;
    this.getOrderlist(p);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})